﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">


  <xsl:output method="xml" indent="yes"/>

  <!-- Begin parameters declaration -->
  <xsl:param name="EnvironmentTarget" select="'DEV'" />
  <xsl:param name="ApplicationTarget" select="'Talend'" />
  <xsl:param name="VersionTarget" select="'1.0'" />
  <!-- End parameters declaration -->


  <!-- Begin : Include xslt transfromation files-->
  <xsl:include href="CrossCuttingTools.xslt" />
  <xsl:include href="CrossCuttingTools_RelativeAttribute.xslt" />
  <xsl:include href="FolderOutputCreation.xslt" />
  <xsl:include href="ServerOutputCreation.xslt" />
  <xsl:include href="TemplateOutputCreation.xslt" />
  <!-- End : Include xslt transfromation files-->

  <!-- Begin : Start deployment configuration creation -->
  <xsl:template match="/">

    <!-- Call deployment target -->
    <xsl:apply-templates select="//deploymentTarget[@environment=$EnvironmentTarget and @applicationName=$ApplicationTarget]" />

  </xsl:template>
  <!-- End : Start deployment configuration creation -->

  <!-- Create the deployment Target to use with applicationManagement Module v2-->
  <xsl:template match="//deploymentTarget">
    <deploymentTarget>
      <!-- Find application definition -->
      <xsl:variable name="application" select="//application[@name=$ApplicationTarget]" />

      <!-- Copy General deployer Configuration -->
      <xsl:apply-templates select="configuration" mode="deploymentV2.0" />
      
      <!-- Search Deployment Node -->
      <xsl:apply-templates select="deploymentNode">
        <xsl:with-param name="application" select="$application" />
      </xsl:apply-templates>

    </deploymentTarget>
  </xsl:template>


  <xsl:template match="logger">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@*" mode="copyAttribute" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="mailing" mode="deploymentV2.0">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="@*" mode="copyAttribute" />
      <xsl:apply-templates select="*" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="mailing/*">
    <xsl:attribute name="{name()}">
      <xsl:variable name="mappingMailing" select="@value" />
      <xsl:value-of select="//usersAndGroups/usersAndGroup[@name=$mappingMailing]/add[@environment=$EnvironmentTarget]/@value" />
    </xsl:attribute>
  </xsl:template>

  <!-- Begin Apply template deploymentNode -->
  <xsl:template match ="//deploymentNode">
    <xsl:param name="application" />

    <xsl:variable name="serverName" select="@serverName" />

    <!-- Find Server configuration -->
    <xsl:variable name="server" select="//server[@name=$serverName]" />

    <xsl:comment>
      <xsl:text>Begin deployment node application </xsl:text>
      <xsl:value-of select="$application/@name"/> version <xsl:value-of select="$application/@version"/> on server <xsl:value-of select="@serverName"/>
    </xsl:comment>
    <server>
      <!-- add server attribute -->
      <xsl:attribute name="hostName">
        <xsl:value-of select="$serverName"/>
      </xsl:attribute>
      <xsl:attribute name="environment">
        <xsl:value-of select="$EnvironmentTarget"/>
      </xsl:attribute>
      
      <!-- Apply other attributes from the deployment node-->
      <xsl:apply-templates select="@*" mode="copyAttribute" />

      <!-- Compatibility Mode : NodeName become server name and it is used to retrieve package-->
      <xsl:attribute name="name">
        <xsl:value-of select="@nodeName"/>
      </xsl:attribute>
      
      <!-- find the template for the server and apply it-->
      <xsl:apply-templates select="$server">
        <xsl:with-param name="server" select="$server" />
        <xsl:with-param name="application" select="$application" />
      </xsl:apply-templates>

      <!-- find the template for the server and deployment model 2.0 and apply it-->
      <xsl:apply-templates select="$server" mode="deploymentV2.0">
        <xsl:with-param name="current" select="$server" />
        <xsl:with-param name="application" select="$application" />
      </xsl:apply-templates>

      <!-- Add components definition for this deployment Node-->
      <!-- add components to be deployed -->
      <xsl:apply-templates select="component">
        <xsl:with-param name="server" select="$server" />
        <xsl:with-param name="application" select="$application" />
      </xsl:apply-templates>
    </server>
    <xsl:comment>
      <xsl:text>End deployment node application </xsl:text>
      <xsl:value-of select="$application/@name"/> version <xsl:value-of select="$application/@version"/> on server <xsl:value-of select="@serverName"/>
    </xsl:comment>
  </xsl:template>


  <!-- Begin Apply component for Server-->
  <xsl:template match="//deploymentNode/component">
    <xsl:param name="server" />
    <xsl:param name="application" />

    <!-- Find component definition-->
    <xsl:variable name="componentName" select="@name" />
    <xsl:variable name="componentDefinition" select="$application/components/component[@name=$componentName]"/>
    <xsl:variable name="componentInstance" select="." />

    <xsl:variable name="middlewareTarget" select="./@middlewareTarget"/>
    <xsl:variable name="applicationPoolTarget" select="./@applicationPoolTarget"/>

    <component>
      <!-- Copy attributes from the component Definition -->
      <xsl:apply-templates select="$componentDefinition/@*" mode="copyAttribute" />

      <!-- Copy attributes from component instance -->
      <xsl:apply-templates select="./@*" mode="copyAttribute" />

      <!-- Add relative Attributes which are calculated with server Middleware execution container -->
      <xsl:apply-templates select="$componentDefinition/relativeAttributes/*" mode="ApplyRelativeAttributeValue">
        <xsl:with-param name="server" select="$server" />
        <xsl:with-param name="application" select="$application" />
        <xsl:with-param name="middlewareTarget" select="$middlewareTarget" />
        <xsl:with-param name="componentDefinition" select="$componentDefinition" />
        <xsl:with-param name="componentInstance" select="$componentInstance" />
      </xsl:apply-templates>

      <!-- add attributes from targeted application pool-->
      <xsl:apply-templates select="$server" mode="FindApplicationPoolTarget">
        <xsl:with-param name="server" select="$server" />
        <xsl:with-param name="applicationPoolTarget" select="$applicationPoolTarget" />
      </xsl:apply-templates>

    </component>

  </xsl:template>
  <!-- End Apply component for Server-->


  <xsl:template match="*" mode="FindApplicationPoolTarget">
    <xsl:param name="server" />
    <xsl:param name="applicationPoolTarget" />

    <xsl:variable name="templateName" select="@template" />
    <xsl:variable name="elementName" select="name()" />

    <xsl:if test="$templateName != ''">
      <xsl:apply-templates select="//templates/template[@type=$elementName and @name= $templateName]/*" mode="FindApplicationPoolTarget">
        <xsl:with-param name="server" select="$server" />
        <xsl:with-param name="applicationPoolTarget"  select="$applicationPoolTarget" />
      </xsl:apply-templates>
    </xsl:if>

    <xsl:choose>
      <xsl:when test="name()='applicationPool' and @applicationPoolName=$applicationPoolTarget">
        <xsl:apply-templates select=".">
          <xsl:with-param name="server" select="$server" />
          <xsl:with-param name="applicationPoolTarget"  select="$applicationPoolTarget" />
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="./*" mode="FindApplicationPoolTarget">
          <xsl:with-param name="server" select="$server" />
          <xsl:with-param name="applicationPoolTarget"  select="$applicationPoolTarget" />
        </xsl:apply-templates>    
      </xsl:otherwise>
    </xsl:choose>               
  </xsl:template>


  <xsl:template match="applicationPool">
    <xsl:param name="server" />
    <xsl:param name="applicationPoolTarget" />


    <!-- Switch to the manifest template-->
    <xsl:call-template name="SwitchToManifestTemplate">
    </xsl:call-template>
    
    <xsl:apply-templates select="@*" mode="copyAttribute" />

    <xsl:apply-templates select="./security/add" mode="deploymentV2.0">      
    </xsl:apply-templates>
  </xsl:template>
  
</xsl:stylesheet>
